//
//  MCViewController.m
//  利用model处理数据
//
//  Created by Mr&Clover on 2019/3/12.
//  Copyright © 2019年 MrC. All rights reserved.
//

#import "MCViewController.h"
#import "MCTableViewCell.h"
#import "MCModel.h"

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface MCViewController ()<UITableViewDelegate, UITableViewDataSource, MCTableViewCellDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation MCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myTableView];
    [self.myTableView registerNib:[UINib nibWithNibName:@"MCTableViewCell" bundle:nil] forCellReuseIdentifier:@"aCell"];
    
}



// 遵循代理
- (void)receiveModel:(MCModel *)model
{
    [self dismissViewControllerAnimated:NO completion:nil];
    if (self.modelBlock) {
        // 传值
        self.modelBlock(model);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aCell"];
    MCModel *model = self.dataSource[indexPath.row];
    // 利用model传值
    cell.model = model;
    // 在这里设置代理
    cell.delegate = self;
    return cell;
}

- (UITableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        NSMutableArray *modelArray = [NSMutableArray array];
        NSArray *array = @[
                           @{
                               @"address": @"山东省青岛市",
                               @"title" : @"海底世界",
                               @"tel" : @"1344569890"
                           },
                           
                           @{
                               @"address": @"江苏南京市",
                               @"title" : @"夫子庙",
                               @"tel" : @"1298076546"
                               },
                           @{
                               @"address": @"上海市",
                               @"title" : @"外滩",
                               @"tel" : @"18890876788"
                               },
                           @{
                               @"address": @"浙江省台州市",
                               @"title" : @"神仙居",
                               @"tel" : @"17890908765"
                               },
                           ];
        
        for (int i = 0; i < array.count; i++) {
            // 对model进行赋值
            NSDictionary *dict = array[i];
            MCModel *model = [[MCModel alloc] init];
            model.title = dict[@"title"];
            model.address = dict[@"address"];
            model.telPhone = dict[@"tel"];
            [modelArray addObject:model];
        }
        _dataSource = [NSMutableArray arrayWithArray:modelArray];
    }
    return _dataSource;
}

@end
